package com.zhaodm.practise.webview;

import com.tcl.matrix.mds.js.Utility;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

@SuppressLint("SetJavaScriptEnabled")
public class WebViewActivity extends Activity {
	
	private static final String TAG = WebViewActivity.class.getSimpleName();
	private WebView webView = null;
	private Button hide = null;
	private Button show = null;
	private Button jump = null;
	private String url = "http://10.0.2.2:8080/webview/webView.html";
	private String error_url = "http://10.0.2.2:8080/webview/error_page.html";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);
        
        webView = (WebView)findViewById(R.id.webView);
        hide = (Button)findViewById(R.id.hide);
        show = (Button)findViewById(R.id.show);
        jump = (Button)findViewById(R.id.jump);

        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        
        Utility utility = new Utility();
        webView.addJavascriptInterface(utility, "Utility");
        
        webView.setWebViewClient(new WebViewClient() {
        	
        	@Override
        	public boolean shouldOverrideUrlLoading(WebView view, String url) {
        		view.loadUrl(url);
        		return true;
        	}
        	
        	@Override
        	public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        		Log.e(TAG, "load error url:"+failingUrl);
        		view.stopLoading();
        		view.clearView();
        		
        		webView.loadUrl(error_url);
        	}
        	
        	@Override
        	public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
        		Log.i(TAG, "web view event = "+event.getKeyCode());
        		return true;
        	}
        });
        webView.setWebChromeClient(new WebChromeClient() {  
        	  
            public void onProgressChanged(WebView view, int progress) {  
                setTitle("页面加载中，请稍候..." + progress + "%");  
                setProgress(progress * 100);  
          
                if (progress == 100) {  
                    setTitle(R.string.app_name);  
                }  
            }  
        });
        
        hide.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				//webView.loadUrl(url);
				webView.setVisibility(View.INVISIBLE);
			}
        	
        });
        
        show.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				webView.setVisibility(View.VISIBLE);
			}
        	
        });
        
        jump.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				webView.loadUrl(url);
			}
        	
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_web_view, menu);
        return true;
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
    	Log.i(TAG, "on key up, dispatch key code = "+keyCode);
    	//webView.loadUrl("javascript:document.onkeydown()");
    	return super.onKeyUp(keyCode, event);
    	
    	//webView.dispatchKeyEvent(event);
    	
    	//return true;
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	
    	Log.i(TAG, "on key down, key code = "+keyCode);
    	switch (keyCode) {
    	case KeyEvent.KEYCODE_BACK:
    	{
    		if (webView != null && webView.canGoBack()) {
    			webView.goBack();
    			return true;
    		}
    		break;
    	}
    	}
    	
    	return super.onKeyDown(keyCode, event);
    }
}
